#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int chk(char c){return 48<=c && c<=57;}
inline int read()
{
	static int x,f;x=0;f=0;
	static char ch;
	while(!chk(ch=getchar())) if(ch=='-') f=1;
	while(chk(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return f?-x:x;
}
const int inf=0x1f1f1f1f1f1f1f1fll;
int t;
int n,ans;
int a[200010];
int lsh[200010],tot;
void initialize()
{
	tot=ans=0;
}
struct SegTree
{
	int mx[800020];
	int tag[800020];
	inline void upload(int p){mx[p]=max(mx[p<<1],mx[p<<1|1]);}
	inline void download(int p)
	{
		if(tag[p])
		{
			mx[p<<1]+=tag[p];tag[p<<1]+=tag[p];
			mx[p<<1|1]+=tag[p];tag[p<<1|1]+=tag[p];
			tag[p]=0;
		}
	}
	void build(int p,int l,int r)
	{
		mx[p]=-inf;
		tag[p]=0;
		if(l==r) return;
		int mid=(l+r)>>1;
		build(p<<1,l,mid);
		build(p<<1|1,mid+1,r);
	}
	void add(int p,int l,int r,int L,int R,int v)
	{
		if(L<=l && r<=R)
		{
			mx[p]+=v;
			tag[p]+=v;
			return;
		}
		download(p);
		int mid=(l+r)>>1;
		if(L<=mid) add(p<<1,l,mid,L,R,v);
		if(mid<R) add(p<<1|1,mid+1,r,L,R,v);
		upload(p);
	}
	void change(int p,int l,int r,int x,int v)
	{
		if(l==r)
		{
			mx[p]=max(mx[p],v);
			return;
		}
		download(p);
		int mid=(l+r)>>1;
		if(x<=mid) change(p<<1,l,mid,x,v);
		else change(p<<1|1,mid+1,r,x,v);
		upload(p);
	}
	int query(int p,int l,int r,int L,int R)
	{
		if(L>R) return -inf;
		if(L<=l && r<=R) return mx[p];
		download(p);
		int mid=(l+r)>>1;
		if(R<=mid) return query(p<<1,l,mid,L,R);
		else if(mid<L) return query(p<<1|1,mid+1,r,L,R);
		else return max(query(p<<1,l,mid,L,mid),query(p<<1|1,mid+1,r,mid+1,R));
	}
};
SegTree dp;
void solve()
{
	n=read();
	initialize();
	for(int i=1;i<=n;i++)
	{
		a[i]=read();
		lsh[++tot]=a[i];
	}
	sort(lsh+1,lsh+tot+1);
	tot=unique(lsh+1,lsh+tot+1)-lsh-1;
	for(int i=1;i<=n;i++) a[i]=lower_bound(lsh+1,lsh+tot+1,a[i])-lsh;
	dp.build(1,0,tot);
	dp.change(1,0,tot,0,0);
	for(int i=1;i<=n;i++)
	{
		int sp1=max(dp.query(1,0,tot,0,a[i]-1),dp.query(1,0,tot,a[i]+1,tot));
		int sp2=dp.query(1,0,tot,a[i],a[i])+lsh[a[i]];
		if(a[i-1]==a[i]) dp.add(1,0,tot,0,tot,lsh[a[i]]);
		dp.change(1,0,tot,a[i-1],max(sp1,sp2));
	}
	printf("%lld\n",dp.query(1,0,tot,0,tot));
}
signed main()
{
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	t=read();
	while(t--) solve();
	//~ cerr<<1.0*clock()/CLOCKS_PER_SEC<<endl;
	return 0;
}
